package com.github.cyf.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.cyf.domain.po.RolePermission;
import com.github.cyf.mapper.RolePermissionMapper;
import com.github.cyf.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色权限关联表 服务实现类
 * </p>
 *
 * @author chenyifan
 * @since 2024-10-16
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRolePermission(Long roleId, List<String> permissionIdList) {
        removeById(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId, roleId));
        List<RolePermission> rolePermissions = new ArrayList<>();
        for (String permissionId : permissionIdList) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(Long.valueOf(permissionId));
            rolePermissions.add(rolePermission);
        }
        return saveBatch(rolePermissions);
    }
}